package com.zou.application.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.zou.application.dao.UserEntityMapper;
import com.zou.application.entity.UserEntity;
import com.zou.application.utils.ConfigUtils;
import com.zou.application.utils.StringUtils;

@Controller
@RequestMapping("/admin")
public class AdminLoginController {
	public static final String adminUsername = ConfigUtils.getPropertiesByKey("admin.username");
	public static final String adminPassword = ConfigUtils.getPropertiesByKey("admin.password");
	
	@Autowired
	private UserEntityMapper userEntityMapper;
	
	@RequestMapping("")
	public String admin(){
		return "/admin/admin_login";
	}
	
	@RequestMapping("/login")
	public String indexPage(){
		return "/admin/admin_login";
	}
	
	/**
	 * 管理员账号登陆控制层
	 * @param request
	 * @return
	 */
	@RequestMapping("/checkPassWord")
	public ModelAndView adminLogin(HttpServletRequest request){
		ModelAndView mv = new ModelAndView();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		if(StringUtils.isEmpty(username)){
			mv.addObject("code","100");
			mv.addObject("message","用户名不能为空");
			mv.setViewName("/admin/admin_login");
			return mv;
		}else if(StringUtils.isEmpty(password)){
			mv.addObject("code","100");
			mv.addObject("message","用户名不能为空");
			mv.setViewName("/admin/admin_login");
			return mv;
		}else if(!adminUsername.equals(username)){
			mv.addObject("code","100");
			mv.addObject("message","用户名不正确");
			mv.setViewName("/admin/admin_login");
			return mv;
		}else if(!adminPassword.equals(password)){
			mv.addObject("code","100");
			mv.addObject("message","密码 不正确");
			mv.setViewName("/admin/admin_login");
			return mv;
		}
		Map<String, Object> param = new HashMap<String, Object>();
		List<UserEntity> userEntities = userEntityMapper.listByMap(param);
		mv.addObject("username",username);
		mv.addObject("users",userEntities);
		mv.setViewName("/admin/user/user_list");
		return mv;
	}
	
	
}
